package com.atung.parking.init;

import com.atung.parking.config.auth.AuthUserMangerContext;
import com.atung.parking.config.auth.PwdCreateUtil;
import com.atung.parking.entity.BusSuperUser;
import com.atung.parking.enums.AdminType;
import com.atung.parking.enums.SuperUserState;
import com.atung.parking.mapper.BusSuperUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author Administrator
 */
@Component
@Log4j2
public class SuperAdminInitializerRunner implements CommandLineRunner {
    @Resource
    private BusSuperUserMapper busSuperUserMapper;

    private final String usename = "superAdmin";
    private final String password = "superAdmin123";

    @Override
    public void run(String... args) throws Exception {
        QueryWrapper<BusSuperUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", usename);
        queryWrapper.eq("name", usename);
        BusSuperUser dbBusSuperUser = busSuperUserMapper.selectOne(queryWrapper);
        if (Objects.isNull(dbBusSuperUser)) {
            log.info("初始化超级管理员");
            BusSuperUser busSuperUser = new BusSuperUser();
            busSuperUser.setId(System.currentTimeMillis());
            busSuperUser.setName(usename);
            busSuperUser.setUsername(usename);
            busSuperUser.setStatus(SuperUserState.NORMAL.ordinal());
            busSuperUser.setDescr("超级管理员初始化");
            busSuperUser.setType(AdminType.SUPERADMIN.ordinal());
            String salt = PwdCreateUtil.createSalt();
            busSuperUser.setSalt(salt);
            busSuperUser.setPassword(PwdCreateUtil.createPwd(password, salt));
            busSuperUser.setCreatorId("初始化超级管理员");
            busSuperUser.setCreatorName("初始化超级管理员");
            busSuperUser.setCreatorTime(String.valueOf(System.currentTimeMillis()));
            busSuperUser.setModifierId("初始化超级管理员");
            busSuperUser.setModifierName("初始化超级管理员");
            busSuperUser.setModifierTime(String.valueOf(System.currentTimeMillis()));
            busSuperUserMapper.save(busSuperUser);
        }
    }


}
